package servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


import dataTypes.*;

/*
 * A user adds a deal coupon
 */
@SuppressWarnings("serial")
public class userAddCoupon extends HttpServlet {

	public userAddCoupon() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		try {
			String googleName=null;
			response.setCharacterEncoding("UTF-8"); 
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Content-Encoding","UTF-8");

			int  bId = Integer.parseInt(request.getParameter("bId"));
			int  dId = Integer.parseInt(request.getParameter("dId"));
			int  num=Integer.parseInt(request.getParameter("num"));
			String googleNameFromAndroid = request.getParameter("uId");
			if (googleNameFromAndroid.equals("notAndroidApp"))
				googleNameFromAndroid = null;

			if (googleNameFromAndroid == null){
				if ((googleName=appManager.getInstance().getGoogleName(request))==null){
					appManager.getInstance().signIn(request, response);
					appManager.getInstance().addUsertoDB(googleName, null, null);}
				else{
					appManager.getInstance().addUsertoDB(googleName, null, null);
					long[] result = appManager.getInstance().userAdd(bId, googleName, dId, num);
					
					doResponse(response, googleName, result[0],result[1]);
				}
				
			}
			else {
				long[] result = appManager.getInstance().userAdd(bId, googleNameFromAndroid, dId, num);
				doResponse(response, googleNameFromAndroid, result[0],result[1]);
			}

		}
		catch(Exception e){
			e.printStackTrace(response.getWriter());

		}
		finally {

		}
	}

	private void doResponse(HttpServletResponse response, String googleName, long result,long moneysaved) throws IOException{
		response.getWriter().println("<html>");
		response.getWriter().println("<head>");
		response.getWriter().println("<title>CoupoNow</title>");

		response.getWriter().println("<link type='text/css' rel='stylesheet' media='screen' href='/jqtouch/themes/jqt/theme.css' charset='utf-8'>");
		response.getWriter().println("<link type='text/css' rel='stylesheet' media='screen' href='/jqtouch/jqtouch/jqtouch.css' charset='utf-8'>");
		response.getWriter().println("<script src='/jqtouch/jqtouch/jquery-1.4.2.js'></script>");
		response.getWriter().println("<script type='application/x-javascript' src='/jqtouch/jqtouch/jqtouch.js' charset='utf-8'></script>");
		response.getWriter().println("<script type='text/javascript'>");
		response.getWriter().println("var jQT =  new $.jQTouch({});");
		response.getWriter().println("</script>");

		response.getWriter().println("</head>");

		response.getWriter().println("<body>");
		response.getWriter().println("<div id=\"jqt\">");
		response.getWriter().println("<div id='home' selected='true'>");
		response.getWriter().println("<div class='toolbar'>");
		response.getWriter().println("<h1><img src='heading1.png' width='110px' height='38px'/></h1>");
		response.getWriter().println("<a href='/?username=" + googleName + "' target='_webapp' class='button leftButton'>חזור</a>");
		response.getWriter().println("</div>");


		if (result == 0){
			response.getWriter().println("<BR>Hello  "+googleName+"  We are sorry But We Are out of the coupons you wanted!! <BR>  ");
		}
		else if (result==1){
			response.getWriter().println("<BR>Hello  "+googleName+"  We are sorry But you already took the maximum amount of coupons for this deal!! <BR>  ");
		}
		else 	response.getWriter().println("<BR>Hello  "+googleName+"  Coupon Purchased! <BR> Your Coupon Number is: " + result);

		response.getWriter().println("</div>");
		response.getWriter().println("</div>");
		response.getWriter().println("</body>");
		response.getWriter().println("</html>");
		if (result>1)
		appManager.getInstance().sendEmail(googleName, result,moneysaved);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
